﻿Public Class Form1

    Dim RoseRTApp As RoseRT.Application
    Dim theModel As RoseRT.Model
    Dim theCapsules As RoseRT.CapsuleCollection
    Dim theCapsule As RoseRT.Capsule
    Dim theStateMachine As RoseRT.StateMachine
    Dim theStateMachineDiagram As RoseRT.StateDiagram
    Dim theTopState As RoseRT.CompositeState

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        'RoseRTApp.Exit()
        System.Threading.Thread.Sleep(1000) 'Give RoseRT time to shut down
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        InitializeRoseRT()
    End Sub

    Public Sub InitializeRoseRT()
        'Create an instance of RoseRT and open the test model
        'RoseRTApp = New RoseRT.Application
        'theModel = RoseRTApp.OpenModel("D:\workarea\X3DUML_old\RoseRTtoX3D\TrafficLights.rtmdl")
        'theModel = RoseRTApp.OpenModel("D:\x3duml\presentations\2008\mirug\actual_model.rtmdl")
        'Log("Model: " + RoseRTApp.CurrentModel.GetFileName)
        'Now process the model to create the visualisation
        'VisualiseDiagram()
        'TestDiagram()
        Me.AxX3DUMLv21.ShowSplashScreen()
        Me.AxX3DUMLv21.RenderDiagram()
        Me.Timer1.Enabled = True
        'Me.AxX3DUMLv21.SaveToOSG("mydiagram.osg")
        'Log(Me.AxX3DUMLv21.CurrentDiagramFullyQualifiedName)
    End Sub

    Public Sub TestDiagram()
        Log("Current Diagram: " + Me.AxX3DUMLv21.CurrentDiagramFullyQualifiedName)
        'Me.AxX3DUMLv21.AddDiagram("Top")
        Me.AxX3DUMLv21.SetOrCreateCurrentDiagram("Root.Top.")
        Dim ElementName As String = Me.AxX3DUMLv21.AddStateView(300, 200, 0, 0, 0)
        Me.AxX3DUMLv21.AddTextView("Top", 0, 0, 0)
        Log("Current Diagram: " + Me.AxX3DUMLv21.CurrentDiagramFullyQualifiedName)
        'Me.AxX3DUMLv21.AddDiagram("SubDiagram1")
        Me.AxX3DUMLv21.SetOrCreateCurrentDiagram("Root.Top.SubDiagram1.")
        Me.AxX3DUMLv21.AddStateView(400, 300, 0, 0, 0) ' The first element is the bounds of the diagram
        Me.AxX3DUMLv21.AddTextView("SubDiagram1", 0, 0, 0)
        Log("Current Diagram: " + Me.AxX3DUMLv21.CurrentDiagramFullyQualifiedName)
        'Me.AxX3DUMLv21.SetOrCreateCurrentDiagram("Root.Top.SubDiagram1.")
        Me.AxX3DUMLv21.SetOrCreateCurrentDiagram("Root.Top.SubDiagram2.")
        Me.AxX3DUMLv21.AddStateView(400, 300, 0, 0, 0)
        Me.AxX3DUMLv21.AddTextView("SubDiagram2", 0, 0, 0)
        Log("Current Diagram: " + Me.AxX3DUMLv21.CurrentDiagramFullyQualifiedName)
        Me.AxX3DUMLv21.LinkElementToDiagram(ElementName, "Root.Top.SubDiagram1.")
        Me.AxX3DUMLv21.LinkElementToDiagram(ElementName, "Root.Top.SubDiagram2.")
        Me.AxX3DUMLv21.RenderDiagram()
        Me.Timer1.Enabled = True
        Log("Test OK")
        Me.AxX3DUMLv21.SaveToOSG("mydiagram.osg")
    End Sub

    Public Sub VisualiseDiagram()
        'Me.AxX3DUMLv21.ShowSplashScreen()
        theCapsules = theModel.GetAllCapsules()
        theCapsule = theCapsules.GetAt(theCapsules.FindFirst("TrafficLightAustrian"))
        'theCapsule = theCapsules.GetAt(theCapsules.FindFirst("FBConfigurationManager"))

        Log("Capsule: " + theCapsule.Name)
        Me.Text = Me.Text + " - " + theCapsule.Name
        theStateMachine = theCapsule.StateMachine
        theStateMachineDiagram = theStateMachine.Diagram
        theTopState = theStateMachine.Top
        'Me.AxX3DUMLv21.SetOrCreateCurrentDiagram(theTopState.Name)
        RecurseStateDiagram("Root." + theTopState.Name + ".", theStateMachineDiagram.ViewElements)

        'RecurseStateDiagram(theStateMachineDiagram.ViewElements)
        'Me.AxX3DUMLv21.AddStateView(10, 100, 10, 10, 10)
        'Me.AxX3DUMLv21.ShowSplashScreen()
        Me.AxX3DUMLv21.RenderDiagram()
        Me.Timer1.Enabled = True
        Log("Test OK")

    End Sub

    Public Sub RecurseStateDiagram(ByVal fullyQualifiedName As String, ByVal theViewElements As RoseRT.ViewElementCollection)
        'Public Sub RecurseStateDiagram(ByVal theViewElements As RoseRT.ViewElementCollection)
        Me.AxX3DUMLv21.SetOrCreateCurrentDiagram(fullyQualifiedName)
        Log("Diagram: " + fullyQualifiedName)
        'Log(fullyQualifiedName)
        'Log(Me.AxX3DUMLv21.SetCurrentDiagram(fullyQualifiedName))
        Dim theViewElement As RoseRT.ViewElement
        Dim theCompositeStateView As RoseRT.CompositeStateView
        Dim i As Short
        For i = 1 To theViewElements.Count
            theViewElement = theViewElements.GetAt(i)
            Select Case theViewElement.IdentifyClass
                Case "StatePerimeterView"
                    Me.AxX3DUMLv21.AddStateView(theViewElement.Width, _
                                                theViewElement.Height, _
                                                theViewElement.XPosition - theViewElement.Width / 2, _
                                                theViewElement.YPosition - theViewElement.Height / 2, _
                                                2)
                    'Log("Adding StatePerimeterView")
                Case "CompositeStateView"
                    Me.AxX3DUMLv21.AddTextView(theViewElement.Name, _
                                                theViewElement.XPosition, _
                                                theViewElement.YPosition, _
                                                -2)
                    Dim ElementName As String = Me.AxX3DUMLv21.AddStateView(theViewElement.Width, _
                                                theViewElement.Height, _
                                                theViewElement.XPosition - theViewElement.Width / 2, _
                                                theViewElement.YPosition - theViewElement.Height / 2, _
                                                0)
                    theCompositeStateView = theViewElement
                    If Not theCompositeStateView.SubDiagram Is Nothing Then
                        RecurseStateDiagram(fullyQualifiedName + theCompositeStateView.Name + ".", theCompositeStateView.SubDiagram.ViewElements)
                        Me.AxX3DUMLv21.LinkElementToDiagram(ElementName, fullyQualifiedName + theCompositeStateView.Name + ".")
                        Me.AxX3DUMLv21.SetCurrentDiagram(fullyQualifiedName)
                    End If
                Case "InitialPointView"
                    Me.AxX3DUMLv21.AddPseudoStateView(theViewElement.Width, _
                                                    theViewElement.Height, _
                                                    theViewElement.XPosition, _
                                                    theViewElement.YPosition, _
                                                    0, _
                                                    "Initial")
                Case "JunctionPointView"
                    Me.AxX3DUMLv21.AddPseudoStateView(theViewElement.Width, _
                                                    theViewElement.Height, _
                                                    theViewElement.XPosition, _
                                                    theViewElement.YPosition, _
                                                    0, _
                                                    "Junction")
                Case "JunctionAdornmentView"
                    Me.AxX3DUMLv21.AddPseudoStateView(theViewElement.Width, _
                                                    theViewElement.Height, _
                                                    theViewElement.XPosition, _
                                                    theViewElement.YPosition, _
                                                    0, _
                                                    "History")
                    'Case "BranchPointView"
                    'Case "ChoicePointView"
                Case "NoteView"
                    Dim theNoteView As RoseRT.NoteView = theViewElement
                    Me.AxX3DUMLv21.AddTextView(theNoteView.Text, theViewElement.XPosition, theViewElement.YPosition, -1)
                Case "ViewElement"
                    If theViewElement.GetPropertyClassName.Contains("StateTransition") Then
                        Dim theLineVertexCollection As RoseRT.LineVertexCollection = theViewElement.LineVertices
                        Dim theLineWayPoint As RoseRT.LineVertex
                        Dim theWayPointCSVString As String = "" 'format for string MUST be "x,y,z,x,y,z,"....
                        Dim w As Short
                        For w = 1 To theLineVertexCollection.Count
                            theLineWayPoint = theLineVertexCollection.GetAt(w)
                            theWayPointCSVString += theLineWayPoint.GetXPosition.ToString + "," + theLineWayPoint.GetYPosition.ToString + ",0,"
                        Next
                        'Log(theWayPointCSVString)
                        Me.AxX3DUMLv21.AddTransitionView(theWayPointCSVString)
                        'Me.AxX3DUMLv21.AddTextView("StateTransition", theViewElement.XPosition, theViewElement.YPosition, 0)
                    End If
                Case Else
                    'Create Text for anything left over
                    Me.AxX3DUMLv21.AddTextView(theViewElement.IdentifyClass, theViewElement.XPosition, theViewElement.YPosition, 0)
            End Select
        Next
    End Sub

    Public Sub Log(ByVal theText As String)
        Me.TextBox1.Text = Me.TextBox1.Text + theText + vbNewLine
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.AxX3DUMLv21.FrameUpdate()
    End Sub
End Class

